/**
 * related to listType.ui
 * 
 * @Author : chenglm
 * @Timestamp : 2016-09-26
 */
var app = sm("do_App");
var page = sm("do_Page");
var hashdata = mm("do_HashData");

var nf = sm("do_Notification");
var dingweiUtil = require("helper/bdmap");
var helperOpen = require("helper/open");
var homemakingUtil = require("url/homemaking");
var qqHttp = require("helper/http");

var listdata = mm("do_ListData");
var listView_bases = ui("listView_bases");
listView_bases.bindItems(listdata);
var noData = [{"template":1,"labelContent":"还没有服务哦"}];

var typeId;
var x='', y='',communityId=null;

//andriod返回键
page.on("back", function() {
    app.closePage();
});
var dingweiDada;
//接受地图页面关闭的时候传递回来的数据
page.on("result", function(data) {
	//deviceone.print("----------:"+JSON.stringify(data));
    /*if (data && data.address){
    	x = data.longitude;
    	y = data.latitude;
    	data.x = data.longitude;
    	data.y = data.latitude;
    	dingweiDada = [{"template": 2, "address" : data.address+" >", "x": data.x, "y": data.y}];
    	refreshAllData();
    }*/
    if (data.nextAddr || data.houseNumber) {
		selectAddress = data;
		x = data.x;
    	y = data.y;
    	communityId = data.communityId;
    	dingweiDada = [{"template": 2, "address" : data.address+" >", "x": data.x, "y": data.y,"communityId":communityId}];
    	refreshAllData();
	}
})

/* 头部 */
var headerAout = ui("do_ALayout_header");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePage()
});
var header = ui("header");
header.bindData(hashdata);

page.on("loaded", function() {
	selectAddress = null;
	var dw = dingweiUtil.getDingwei();
	x = dw.longitude;
	y = dw.latitude;
	dw.x = dw.longitude;
	dw.y = dw.latitude;
	//dingweiDada = [{"template": 2, "dingwei":{"centerView":{"address":dw.address+" >", "xy":dw}}}];
	dingweiDada = [{"template": 2, "address" : dw.address+" >", "x": dw.x, "y": dw.y,"communityId":""}];
	var data = page.getData();
	typeId = data.typeId;
	if (!typeId || typeId != "") {
		refreshAllData();
	}
})

var pageNum = 1, totalPages = 1;
function refreshAllData(){
	listdata.removeAll();
	listdata.addData(dingweiDada);//图片
	pageNum = 1;
	qqHttp.post(homemakingUtil.url.base.page, {page:pageNum,typeId:typeId,sortType:1,x:x,y:y,communityId:communityId}, function(data) {
		listView_bases.rebound();
		totalPages = data.page.totalPages;
		var title = "服务";
		if (data.page.content.length > 0) {
			for (var i = 0; i < data.page.content.length; i++) {
				data.page.content[i].typeId=typeId;
			}
			title = data.page.content[0].typeStr;
			listdata.addData(data.page.content);
		} else {
			listdata.addData(noData);
		}
		hashdata.addData({
		    "labelTitle" : title
		});
		header.refreshData();
		listView_bases.refreshItems();
	});
}
function getNextPageData(){
	pageNum++;
	if (pageNum <= totalPages) {
		qqHttp.post(homemakingUtil.url.base.page, {page:pageNum,typeId:typeId,sortType:1,x:x,y:y,communityId:communityId}, function(data) {
			totalPages = data.page.totalPages;
			listView_bases.rebound();
			listdata.addData(data.page.content);
			listView_bases.refreshItems();
		})
	} else {
		nf.toast("已是最后一页");
		listView_bases.rebound();
	}
}

//下拉列表，刷新数据
listView_bases.on("pull", function(data){
	//其中state=0：表示开始下拉headerview，；state=1：表示下拉headerview超过headerview的高度，触发一次这个事件；state=2：下拉超过一定值，触发state=1事件后，松手会触发一次这个事件，数据加载完后需要调用rebound方法让header复位
	if (data.state == 2){
		refreshAllData();
	}
});
//上拉列表，翻页数据
listView_bases.on("push", function(data){
	//其中state=0：表示开始上推headerview，；state=1：表示上推headerview超过headerview的高度，触发一次这个事件；state=2：上推超过一定值，触发state=1事件后，松手会触发一次这个事件，数据加载完后需要调用rebound方法让header复位
	if (data.state == 2){		
		getNextPageData();
	}
});

//点击一条
/*listView_bases.on("touch", function(data){
	var one = listdata.getOne(data);
	helperOpen.open("source://view/homemaking/detail/toOrder2.ui",{id:one.id},"","pay_product_back");
});*/